package com.autowite.autowite.listener;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.net.URI;

@Component
public class StartupListener implements ApplicationListener<ContextRefreshedEvent> {

    @Value("${server.port}")
    private int serverPort;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 构造访问 index.html 的 URL
        String url = "http://localhost:" + serverPort + "/html/index.html";

        // 检查当前环境是否支持 Desktop 操作
        if (Desktop.isDesktopSupported()) {
            Desktop desktop = Desktop.getDesktop();
            if (desktop.isSupported(Desktop.Action.BROWSE)) {
                try {
                    desktop.browse(new URI(url));
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("无法自动打开浏览器，请手动访问: " + url);
                }
            } else {
                System.out.println("当前环境不支持打开浏览器，请手动访问: " + url);
            }
        } else {
            System.out.println("当前环境不支持 Desktop 操作，请手动访问: " + url);
        }
    }
}


